package com.example.forum2.services.impl;

import com.example.forum2.common.AppResult;
import com.example.forum2.common.ResultCode;
import com.example.forum2.dao.BoardMapper;
import com.example.forum2.exception.ApplicationException;
import com.example.forum2.model.Board;
import com.example.forum2.services.IBoardService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
@Slf4j
@Service
public class BoardServiceImpl implements IBoardService {
    @Autowired
    private BoardMapper boardMapper;

    @Override
    public List<Board> selectByNum(Integer num) {
//        首先还是做非空校验
        if(num<=0){
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
//            抛出异常
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }
//        调用dao中的类来查询数据
        List<Board> list=boardMapper.selectByNum(num);
        return list;
    }

    @Override
    public void addOneArticleCountById(Long id) {
//  还是先做非空校验
        if(id<=0||id==null){
//            打印日志
            log.warn(ResultCode.FAILED_BOARD_ARTICLE_COUNT.toString());
//            抛出异常
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_BOARD_ARTICLE_COUNT));
        }
//        如果,id不为空,根据id查询板块
        Board board=boardMapper.selectByPrimaryKey(id);
//        对查询到的板块进行校验
        if(board==null){
//            打印日志
            log.warn(ResultCode.ERROR_IS_NULL.toString());
//            抛出异常
            throw new ApplicationException(AppResult.failed(ResultCode.ERROR_IS_NULL));
        }
//检查完成之后,进行更新帖子操作
        Board board1=new Board();
        board1.setId(board.getId());
        board1.setArticleCount(board.getArticleCount()+1);
       int row= boardMapper.updateByPrimaryKeySelective(board1);
        if(row!=1){
            log.warn(ResultCode.FAILED.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED));
        }

    }

    @Override
    public Board selectById(Long id) {
//        首先还是对id,进行校验
        if(id==null||id<=0){
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_CREATE));
        }

        return boardMapper.selectByPrimaryKey(id);
    }

    @Override
    public void subOneArticleCountById(Long id) {
        //  还是先做非空校验
        if(id<=0||id==null){
//            打印日志
            log.warn(ResultCode.FAILED_BOARD_ARTICLE_COUNT.toString());
//            抛出异常
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_BOARD_ARTICLE_COUNT));
        }
        //        如果,id不为空,根据id查询板块
        Board board=boardMapper.selectByPrimaryKey(id);
//        对查询到的板块进行校验
        if(board==null){
//            打印日志
            log.warn(ResultCode.ERROR_IS_NULL.toString());
//            抛出异常
            throw new ApplicationException(AppResult.failed(ResultCode.ERROR_IS_NULL));
        }
        Board board1=new Board();
        board1.setId(board.getId());
        board1.setArticleCount(board.getArticleCount()-1);
//        判断减一之后是否为负数
        if(board1.getArticleCount()<0){
            board1.setArticleCount(0);
        }
        int row=boardMapper.updateByPrimaryKeySelective(board1);
        if(row!=1){
            log.warn(ResultCode.FAILED.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED));
        }
    }
}
